import axios from 'axios'
import { Toast } from 'vant';
//import { getCookie } from './util.js'
import router from '../src/router/index.js';
// axios 配置
axios.defaults.withCredentials = true; //让ajax携带cookie
axios.defaults.timeout = 5000;
axios.defaults.headers.post['Access-Control-Allow-Origin'] = "*";
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8; charset=UTF-8';
// 在 request 拦截器实现
axios.interceptors.request.use((config) => {
  // 单个网站的admin用户
  config.withCredentials = true // 允许携带token ,这个是解决跨域产生的相关问题
  config.timeout = 5000
  const customertoken = localStorage.getItem("customertoken") //获取Cookie
  if (customertoken && customertoken != null) {
    config.headers.common['customertoken'] = customertoken
    // 整个网点user
    config.headers.common['customertoken'] = customertoken
  }

  return config
}, (err) => {
  if (err.response) {
    switch (err.response.status) {
      case 401:
        router.replace({
          path: '/login'
        })
    }
  }
  return Promise.reject(err)
})
// 返回状态判断
axios.interceptors.response.use((res) => {   
  const customertoken=res.headers.customertoken;
  if(customertoken){
   localStorage.setItem("customertoken", customertoken)
   router.replace({
    path: router.currentRoute.fullPath
  })
  }
  const response = res.data
  if (response.msg) {
    if (response.stCode == 0) {
      //Message.success(response.msg)
    } else {
      Toast.fail(response.msg)
    }
  }
  return response
 
}, (err) => {
  if (err && err.response) {
    switch (err.response.status) {
      case 401:
        localStorage.removeItem('customertoken')
        Toast.fail('请登录!')
        router.replace({
          path: '/login',
          query:{
            redirect:router.currentRoute.fullPath
          }
        })
        break
      case 403:
        localStorage.removeItem('customertoken')
        Toast.fail('账户只能唯一登陆!')
        this.$router.push("/login");
        break
      case 404:
        Toast.fail('接口请求异常: ' + err.response.config.url + ', 请重试!')
        break
      case 409:
        Toast.fail('	验证错误 请重试!')
        break
        case 444:
          localStorage.removeItem('customertoken')
          router.replace({
            path: '/login'
          })
          Toast.fail('账号到期 或 不存在!')
          break
       case 445:
        localStorage.removeItem('customertoken')
        router.replace({
          path: '/login'
        })
        Toast.fail('账号错误！')
        break
        case 446:
          localStorage.removeItem('customertoken')
          router.replace({
            path: '/login'
          })
          Toast.fail('未识别身份！')
          break
          case 447:
            localStorage.removeItem('customertoken')
            router.replace({
              path: '/login'
            })
            Toast.fail('未知异常！')
            break
        case 500:
          Toast.fail(err.response.data.message + ', 请重试!')
          break
      default:
        Toast.fail('Oops,接口出错啦!')
    }
  }
  return Promise.reject(err)
})




export default function request (method, url, data, isJson) {
  // 处理请求的url和数据
  //debugger
  data = method == 'get' ? { params: data } : data
  if (isJson) {
 
    axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
  }
  // 发送请求
  return new Promise((resolve, reject) => {
    axios[method](url, data)
      .then(response => {
        resolve(response)
      }, error => {
        resolve(error)
      })
      .catch(error => {
        reject(error)
      })
  })
    .catch(error => {
      console.log(error)
    })
}



